﻿using System;
using FireStarter.Base;
using Firestarter.DB.Schema.BO;
using FireStarterModeller.UI;
using FireStarterModeller.UI.DatabaseConnection;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using Habanero.Faces.Base;
using Habanero.Faces.Test.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.DatabaseConnection
{
    [TestFixture]
    public class TestDatabaseConnectionInfoEditorControl
    {
        [TestFixtureSetUp]
        public void SetUpFixture()
        {
            GlobalUIRegistry.ControlFactory = new ControlFactoryWin();
            ClassDef.ClassDefs.Clear();
            ClassDef.ClassDefs.Add(new XmlClassDefsLoader(BOBroker.GetClassDefsXml(), new DtdLoader()).LoadClassDefs());
        }

        [SetUp]
        public void Setup()
        {
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        private static DatabaseConnectionInfoEditorControl CreateDatabaseConnectionInfoEditorControl()
        {
            var controlFactory = GlobalUIRegistry.ControlFactory;
            return new DatabaseConnectionInfoEditorControl(controlFactory);
        }

        [Test]
        [Ignore("For Visual Testing")]
        
        public void Test__Visually()
        {
            //---------------Set up test pack-------------------
            var control = CreateDatabaseConnectionInfoEditorControl();
            var databaseConnectionInfo = TestUtilsDatabaseConnectionInfo.CreateUnsavedValidDatabaseConnectionInfo();
            control.BusinessObject = databaseConnectionInfo;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            TestUtilsUI.ShowInVisualTestingForm(GlobalUIRegistry.ControlFactory, control, DockStyle.Fill);
            //---------------Test Result -----------------------
        }

        [Test]
        public void Test__Supports_IBOEditorControl()
        {
            //---------------Set up test pack-------------------
            var controlFactory = GlobalUIRegistry.ControlFactory;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var control = new DatabaseConnectionInfoEditorControl(controlFactory);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf<IBOEditorControl>(control);
        }

        [Test]
        public void Test_Construct_ShouldBeAUserControl()
        {
            //---------------Set up test pack-------------------
            var controlFactory = GlobalUIRegistry.ControlFactory;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var control = new DatabaseConnectionInfoEditorControl(controlFactory);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf<UserControlWin>(control);
        }

        [Test]
        public void Test_BusinessObject_GetAndSet_WithDatabaseConnectionInfo()
        {
            //---------------Set up test pack-------------------
            var control = CreateDatabaseConnectionInfoEditorControl();
            var databaseConnectionInfo = TestUtilsDatabaseConnectionInfo.CreateUnsavedValidDatabaseConnectionInfo();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            control.BusinessObject = databaseConnectionInfo;
            //---------------Test Result -----------------------
            Assert.AreSame(databaseConnectionInfo, control.BusinessObject);
        }

        [Test]
        public void Test_BusinessObject_WhenSetWithNonDatabaseConnectionInfo_ShouldThrowError()
        {
            //---------------Set up test pack-------------------
            var control = CreateDatabaseConnectionInfoEditorControl();
            var dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var exception = Assert.Throws<InvalidCastException>(() => control.BusinessObject = dmClass);
            //---------------Test Result -----------------------
            Assert.IsNotNull(exception);
        }

        [Test]
        public void Test_ControlState_WhenVendorSetToNull_ShouldShowNoVendorSelectedControl()
        {
            //---------------Set up test pack-------------------
            var control = CreateDatabaseConnectionInfoEditorControl();
            var databaseConnnectionInfo = TestUtilsDatabaseConnectionInfo.CreateUnsavedValidDatabaseConnectionInfo();
            control.BusinessObject = databaseConnnectionInfo;
            //---------------Assert Precondition----------------
            Assert.IsNotNullOrEmpty(control.DatabaseConnectionInfo.Vendor);
            Assert.IsFalse(control.NoVendorSelectedPanel.Visible);
            //---------------Execute Test ----------------------
            databaseConnnectionInfo.Vendor = null;
            //---------------Test Result -----------------------
            Assert.IsNullOrEmpty(control.DatabaseConnectionInfo.Vendor);
            Assert.IsTrue(control.NoVendorSelectedPanel.Visible);
            Assert.IsFalse(control.FileBasedDatabasePanel.Visible);
            Assert.IsFalse(control.RemoteDatabasePanel.Visible);
        }

        [Test]
        public void Test_ControlState_WhenVendorSetToAccess_ShouldShowFileBasedControl()
        {
            //---------------Set up test pack-------------------
            var control = CreateDatabaseConnectionInfoEditorControl();
            var databaseConnnectionInfo = TestUtilsDatabaseConnectionInfo.CreateUnsavedValidDatabaseConnectionInfo();
            databaseConnnectionInfo.Vendor = "";
            control.BusinessObject = databaseConnnectionInfo;
            //---------------Assert Precondition----------------
            Assert.IsNullOrEmpty(control.DatabaseConnectionInfo.Vendor);
            Assert.IsTrue(control.NoVendorSelectedPanel.Visible);
            Assert.IsFalse(control.FileBasedDatabasePanel.Visible);
            Assert.IsFalse(control.RemoteDatabasePanel.Visible);
            //---------------Execute Test ----------------------
            databaseConnnectionInfo.Vendor = "Access";
            //---------------Test Result -----------------------
            Assert.IsNotNullOrEmpty(control.DatabaseConnectionInfo.Vendor);
            Assert.IsFalse(control.NoVendorSelectedPanel.Visible);
            Assert.IsTrue(control.FileBasedDatabasePanel.Visible);
            Assert.IsFalse(control.RemoteDatabasePanel.Visible);
        }

        [Test]
        public void Test_ControlState_WhenVendorSetToAccess2007_ShouldShowFileBasedControl()
        {
            //---------------Set up test pack-------------------
            var control = CreateDatabaseConnectionInfoEditorControl();
            var databaseConnnectionInfo = TestUtilsDatabaseConnectionInfo.CreateUnsavedValidDatabaseConnectionInfo();
            databaseConnnectionInfo.Vendor = "";
            control.BusinessObject = databaseConnnectionInfo;
            //---------------Assert Precondition----------------
            Assert.IsNullOrEmpty(control.DatabaseConnectionInfo.Vendor);
            Assert.IsTrue(control.NoVendorSelectedPanel.Visible);
            Assert.IsFalse(control.FileBasedDatabasePanel.Visible);
            Assert.IsFalse(control.RemoteDatabasePanel.Visible);
            //---------------Execute Test ----------------------
            databaseConnnectionInfo.Vendor = "Access2007";
            //---------------Test Result -----------------------
            Assert.IsNotNullOrEmpty(control.DatabaseConnectionInfo.Vendor);
            Assert.IsFalse(control.NoVendorSelectedPanel.Visible);
            Assert.IsTrue(control.FileBasedDatabasePanel.Visible);
            Assert.IsFalse(control.RemoteDatabasePanel.Visible);
        }

        [Test]
        public void Test_ControlState_WhenVendorSetToSqlServer_ShouldShowRemoteDatabaseControl()
        {
            //---------------Set up test pack-------------------
            var control = CreateDatabaseConnectionInfoEditorControl();
            var databaseConnnectionInfo = TestUtilsDatabaseConnectionInfo.CreateUnsavedValidDatabaseConnectionInfo();
            databaseConnnectionInfo.Vendor = "";
            control.BusinessObject = databaseConnnectionInfo;
            //---------------Assert Precondition----------------
            Assert.IsNullOrEmpty(control.DatabaseConnectionInfo.Vendor);
            Assert.IsTrue(control.NoVendorSelectedPanel.Visible);
            Assert.IsFalse(control.FileBasedDatabasePanel.Visible);
            Assert.IsFalse(control.RemoteDatabasePanel.Visible);
            ////---------------Execute Test ----------------------
            databaseConnnectionInfo.Vendor = "SQLServer";
            ////---------------Test Result -----------------------
            Assert.IsNotNullOrEmpty(control.DatabaseConnectionInfo.Vendor);
            Assert.IsFalse(control.NoVendorSelectedPanel.Visible);
            Assert.IsFalse(control.FileBasedDatabasePanel.Visible);
            Assert.IsTrue(control.RemoteDatabasePanel.Visible);
        }

        [Test]
        public void Test_ControlState_WhenVendorSetToMySql_ShouldShowRemoteDatabaseControl()
        {
            //---------------Set up test pack-------------------
            var control = CreateDatabaseConnectionInfoEditorControl();
            var databaseConnnectionInfo = TestUtilsDatabaseConnectionInfo.CreateUnsavedValidDatabaseConnectionInfo();
            databaseConnnectionInfo.Vendor = "";
            control.BusinessObject = databaseConnnectionInfo;
            //---------------Assert Precondition----------------
            Assert.IsNullOrEmpty(control.DatabaseConnectionInfo.Vendor);
            Assert.IsTrue(control.NoVendorSelectedPanel.Visible);
            Assert.IsFalse(control.FileBasedDatabasePanel.Visible);
            Assert.IsFalse(control.RemoteDatabasePanel.Visible);
            ////---------------Execute Test ----------------------
            databaseConnnectionInfo.Vendor = "MySQL";
            ////---------------Test Result -----------------------
            Assert.IsNotNullOrEmpty(control.DatabaseConnectionInfo.Vendor);
            Assert.IsFalse(control.NoVendorSelectedPanel.Visible);
            Assert.IsFalse(control.FileBasedDatabasePanel.Visible);
            Assert.IsTrue(control.RemoteDatabasePanel.Visible);
        }

    }
}
